package payrechargelogic

import (
	"context"

	model "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/model/recharge"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/pkg/xerrors"

	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RechargeChannelUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRechargeChannelUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RechargeChannelUpdateLogic {
	return &RechargeChannelUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RechargeChannelUpdateLogic) RechargeChannelUpdate(in *rpc.RechargeChannelUpdateRequest) (*rpc.RechargeChannelUpdateResponse, error) {
	var commonResp rpc.CommonResp

	updateData := make(map[string]interface{})
	if in.MappingCode != "" {
		updateData["mapping_code"] = in.MappingCode
	}
	if in.Type != 0 {
		updateData["type"] = in.Type
	}
	if in.Name != "" {
		updateData["name"] = in.Name
	}
	if in.Level != "" {
		updateData["level"] = in.Level
	}
	if in.UserGroup != "" {
		updateData["user_group"] = in.UserGroup
	}
	if in.BonusRate != 0 {
		updateData["bonus_rate"] = in.BonusRate
	}
	if in.MinAmount != 0 {
		updateData["min_amount"] = in.MinAmount
	}
	if in.MaxAmount != 0 {
		updateData["max_amount"] = in.MaxAmount
	}
	if in.AmountConfig != "" {
		updateData["amount_config"] = in.AmountConfig
	}
	if in.MerchantId != "" {
		updateData["merchant_id"] = in.MerchantId
	}
	if in.AppId != "" {
		updateData["app_id"] = in.AppId
	}
	if in.AppCode != "" {
		updateData["app_code"] = in.AppCode
	}
	if in.SecretKey != "" {
		updateData["secret_key"] = in.SecretKey
	}
	if in.Url != "" {
		updateData["url"] = in.Url
	}
	if in.ReturnUrl != "" {
		updateData["return_url"] = in.ReturnUrl
	}
	if in.NotifyUrl != "" {
		updateData["notify_url"] = in.NotifyUrl
	}
	if in.PubKey != "" {
		updateData["pub_key"] = in.PubKey
	}
	if in.PrivateKey != "" {
		updateData["private_key"] = in.PrivateKey
	}
	if in.StartTime != 0 {
		updateData["start_time"] = in.StartTime
	}
	if in.EndTime != 0 {
		updateData["end_time"] = in.EndTime
	}
	if in.Sort != 0 {
		updateData["sort"] = in.Sort
	}
	if in.Status != 0 {
		updateData["status"] = in.Status
	}
	if in.IsTestable != 0 {
		updateData["is_testable"] = in.IsTestable
	}
	if in.WhiteList != "" {
		updateData["white_list"] = in.WhiteList
	}
	if in.Remark != "" {
		updateData["remark"] = in.Remark
	}
	if in.Icon != "" {
		updateData["icon"] = in.Icon
	}
	if in.ChannelName != "" {
		updateData["channel_name"] = in.ChannelName
	}
	if in.Inr != "" {
		updateData["inr"] = in.Inr
	}

	if len(updateData) == 0 {
		commonResp.Code = xerrors.ParameterError
		commonResp.Message = "No fields to update"
		return &rpc.RechargeChannelUpdateResponse{CommonResp: &commonResp}, nil
	}

	err := model.NewRechargeChannelModel(l.ctx, l.svcCtx.Db).UpdateRechargeChannel(in.Id, updateData)
	if err != nil {
		commonResp.Code = xerrors.DBError
		commonResp.Message = "Database operation error"
		return &rpc.RechargeChannelUpdateResponse{CommonResp: &commonResp}, err
	}

	commonResp.Message = "Update successful"
	return &rpc.RechargeChannelUpdateResponse{CommonResp: &commonResp}, nil
}
